import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.internal.JsonContext;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.stream.Collectors;

public class JsonPathTestMain {

    public static void main(String[] args) {
        String jsonStr = "{" +
                "\"jccjsj\":\"bjsfjc\"," +
                "\"jssbsj\":\"bjsfjc\"" +
                "}";

        StringBuffer jzzlJson = new StringBuffer();
        //读取验证配置
        try {
            InputStream inputStream = JsonPathTestMain.class.getResourceAsStream("/jzzl/GS/GS_JC_SSJC.json");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                jzzlJson.append(line);
            }
            bufferedReader.close();
            inputStream.close();
        }catch (Exception e){
            System.out.println("找不到schema文件：       "+e.getMessage());
            e.printStackTrace();
        }

        List<String> paths = JsonPath
                .using(Configuration.builder().options(Option.AS_PATH_LIST).build())
                .parse(jzzlJson.toString())
                .read("$..ruleType", List.class);
        //查询所有的  $..*
        //只查询ruleType的  $..*

        // compile paths
        List<JsonPath> jsonPaths = paths
                .stream()
                .map(p -> JsonPath.compile(p))
                .collect(Collectors.toList());

        JsonContext document1 = (JsonContext) JsonPath.parse(jzzlJson.toString());

        JsonContext document = (JsonContext) JsonPath.parse(jsonStr);
        jsonPaths.forEach(path -> {
            String pathStr =path.getPath();
            System.out.println(">>>      " + pathStr);
            String ruleType = document1.read(path,String.class);
            System.out.println("ruleType >>>>>>>>>>>>>>>>>>>> " + ruleType);
            //获得父节点
            String parentpath = pathStr.replace("['ruleType']","");
            System.out.println(">>>      " +parentpath);
            JsonPath p = JsonPath.compile("$.jcsbsj");   //根据表达式找到该节点
            //原来值不知道什么类型
            Object value = document.read(p);
            System.out.println("原来的值   >>>>>>>>>>>>>>>>>>>>   " + value);
            document.set(p, transform(value));
        });

        System.out.println(document.jsonString());
    }

    private static Object transform(Object value) {
        return "true";
    }


}
